package Model;

import Database.AccountDB;
import Entity.Account;
import Entity.AccountDetails;
import Entity.Duration;
import Manager.AccountManager;
import Manager.DurationManager;
import Manager.Manager;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import javax.swing.JOptionPane;
import javax.swing.table.AbstractTableModel;

/**
 *
 * @author CONG
 */
public class DurationTableModel extends AbstractTableModel {

    private DurationManager dm;
    private Manager manager;
    private AccountDB dbAccount;
    public DurationTableModel(Manager manager,AccountDB db) {
        this.manager = manager;
        this.dbAccount=db;
    }

    @Override
    public int getRowCount() {
        return manager.getDurationManager().getSize();
    }

    @Override
    public int getColumnCount() {
        return 6;
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        Duration dur = manager.getDurationManager().getDuration(rowIndex);
        SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
        AccountDetails accde = null;
        Account acc = null;
        int accountLoanCode = dur.getAccountLoanCode();
        for (int i = 0; i < manager.getAccountDetailsManager().getSize(); i++) {
            if (manager.getAccountDetailsManager().getAccountDetails(i).getAccountLoanCode() == accountLoanCode) {
                accde = manager.getAccountDetailsManager().getAccountDetails(i);
            }
        }
        if (accde != null) {
            int registerNo = accde.getRegisterNo();
            ArrayList<Account> listAcc=dbAccount.getAccountList();
            for (int i = 0; i < listAcc.size(); i++) {
                int rollNo = listAcc.get(i).getResgisterNo();
                if (registerNo == rollNo) {
                    acc =listAcc.get(i);
                }

            }
        }
        switch (columnIndex) {
            case 0:
                return dur.getLoanTypeCode();
            case 1:
                if(acc!=null) return acc.getName();
                else return "No name";
            case 2:
                String startDate=dateFormat.format(dur.getStartDate());
                return startDate;
            case 3:
                String endDate=dateFormat.format(dur.getEndDate());
                return endDate;
            case 4:
                return dur.getAmountLoan();
            case 5:
                return dur.getBalance();
            case 6:
                return dur.isStatus();
            case 7:
                return dur.isAccepted();
            default:
                return null;
        }
    }

    @Override
    public String getColumnName(int column) {
        switch (column) {
            case 0:
                return "LoanID";
            case 1:
                return "User Name";
            case 2:
                return "Start Date";
            case 3:
                return "End Date";
            case 4:
                return "Loan Amount";
            case 5:
                return "Balance";
            default:
                return null;
        }
    }

    public int getSize() {
        return manager.getDurationManager().getSize();
    }

    public Duration getDuration(int index) {
        return manager.getDurationManager().getDuration(index);
    }

    public void getDurationList(ArrayList<Duration> dur) {
        manager.getDurationManager().getDurationList(dur);
    }
    public boolean addDuration(Duration dur){
        return manager.getDurationManager().addDuration(dur);
    }
    public boolean deleteDuration(Duration dur){
        return manager.getDurationManager().deleteDuration(dur);
    }
}
